﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Library;
using BusinessLayer;

public partial class Administrator_CategoryEdit : System.Web.UI.Page
{
    string cmd = "";
    string Id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = Request.QueryString["cmd"].ToString();
        switch (cmd)
        {
            case "new":
                rbStatus.Enabled = false;
                txtMaDM.Enabled = true;
                txtTen.Enabled = true;
                txtMota.Enabled = true;
                btnNew.Enabled = true;
                btnUpdate.Enabled = false;
                break;
            case "edit":
                txtMaDM.Enabled = false;
                txtTen.Enabled = true;
                txtMota.Enabled = true;
                rbStatus.Enabled = true;
                btnNew.Enabled = false;
                btnUpdate.Enabled = true;
                Id = Request.QueryString["Id"].ToString();
                if (!IsPostBack)
                {
                    FillData(Category.GetOne(Id));
                }
                break;
        }
    }
protected void  btnNew_Click(object sender, EventArgs e)
{
    Category cate = new Category();
    cate.Category_ID = txtMaDM.Text;
    cate.Categoroy_Name = txtTen.Text;
    cate.Detail = txtMota.Text;
    cate.Parent_Category_ID = DropDownList1.SelectedValue;
    try
    {
        Category.Insert(cate);
        Response.Redirect("CategoryManager.aspx");
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
protected void  btnUpdate_Click(object sender, EventArgs e)
{
    Category cate = new Category();
    cate.Parent_Category_ID = DropDownList1.SelectedValue;
    cate.Category_ID = Id;
    cate.Categoroy_Name = txtTen.Text;
    cate.Detail = txtMota.Text;
    try
    {
        Category.Update(cate);
        Response.Redirect("CategoryManager.aspx?cmd=edit&Id=" + Id);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
protected void  BbtnBack_Click(object sender, EventArgs e)
{
    Response.Redirect("CategoryManager.aspx");
}
protected void FillData(Category cate)
{
    DropDownList1.Items.FindByValue(cate.Parent_Category_ID.ToString());
    txtTen.Text = cate.Categoroy_Name;
    txtMota.Text = cate.Detail;
    rbStatus.Items.FindByValue(cate.Status.ToString()).Selected = true;
}
}